export class ModalUser2ViewController {
  constructor ($scope, $log, $http, $timeout, $state, $stateParams, toastr, sidebarGroup, cfg, role, user2, RSAKEY, profile, $uibModal, deploy) {
    'ngInject';


    var self = this;

    self.cfg = cfg;
    self.$http = $http;
    self.toastr = toastr;

    self.type = self.resolve.type;
    self.info = self.resolve.info;
    self.namelists = self.resolve.namelists;


    $scope.type = self.type;
    $scope.info = self.info;
    $scope.namelists = self.namelists;


    $scope.wangbaIdArray;
    $scope.wangbaIdString;
    $scope.nameArray;


    if (self.type == 'edit') {
        self.getData($scope, $log, $timeout, deploy);
    } else if (self.type == 'create') {
        self.getData($scope, $log, $timeout, deploy);
    }


  }

  getData($scope, $log, $timeout, deploy) {
      this.getWangBarName($scope, $log, $timeout, deploy);
  }

  cancel() {
    this.dismiss({$value: 'cancel'});
  }

  ok(result) {

    this.close({$value: result});
  }

  getWangBarName($scope, $log, $timeout, deploy) {
    deploy.getName().then((data)=> {
      if(data) {
        var self = this;
        $scope.checked = [];
        $scope.name = [];
        $scope.deploys = deploy.wrapperAll(data);

        var sMap = {};

        for(var i = 0; i < $scope.namelists.length; i++) {
          sMap[$scope.namelists[i].id] = true;
        }
        $scope.pagelist = $scope.deploys;

        for(var i =0; i< $scope.pagelist.length;i++) {
          if(sMap[$scope.pagelist[i].id]) {
              $scope.pagelist[i].ch = true;
              $scope.checked.push($scope.pagelist[i].id);
              $scope.name.push($scope.pagelist[i].name);
          }
        }

        self.wangbaIdString = $scope.checked;
        self.nameArray = $scope.name;
        self.wangbaIdArray = $scope.checked.join(',');


          $scope.check = function(page, item) {
          if(item) {
            if($scope.checked.indexOf(page.id)>-1){
              $scope.checked.splice($scope.checked.indexOf(page.id),1);
              $scope.name.splice($scope.name.indexOf(page.name),1);
            } else{
              $scope.checked.push(page.id);
              $scope.name.push(page.name);
            }
          } else{
            if($scope.checked.indexOf(page.id)>-1){
              $scope.checked.splice($scope.checked.indexOf(page.id),1);
              $scope.name.splice($scope.name.indexOf(page.name),1);
            } else{
              $scope.checked.push(page.id);
              $scope.name.push(page.name);
            }
          }
          self.wangbaIdString = $scope.checked;
          self.nameArray = $scope.name;
          self.wangbaIdArray = $scope.checked.join(',');


          var myarry = [];
          for(var i = 0; i < self.wangbaIdString.length; i++) {
              myarry.push({
                  id: self.wangbaIdString[i],
                  name: self.nameArray[i]
              });
          }
          $scope.namelists = myarry;

        }
        $scope.checksure = function() {

          var myarry = [];
          for(var i = 0; i < self.wangbaIdString.length; i++) {
              myarry.push({
                  id: self.wangbaIdString[i],
                  name: self.nameArray[i]
              });
          }
          $scope.namelists = myarry;

          console.log($scope.namelists);

          self.ok($scope.namelists)

        }

      }
    });
  }

  // initForm($scope, $log, toastr, RSAKEY, $uibModal, cfg, $timeout, role, deploy) {
  //   var self = this;
  //
  //   $scope.deleteSubmit = function(id) {
  //     $log.log('delete： ' + id);
  //
  //     var modalInstance = $uibModal.open({
  //       animation: false,
  //       component: 'modalComponentConfirm',
  //       backdrop: 'static',
  //       windowClass: 'modal-tips'
  //     });
  //
  //     modalInstance.result.then(function (result) {
  //       $log.log(result);
  //       if(result === 'ok') {
  //
  //         self.$http({
  //           url: self.cfg.api.wangbaUsers.delete.url,
  //           method: self.cfg.api.wangbaUsers.delete.type,
  //           params: self.preParams('delete', $scope.info, encrypt)
  //         }).then((response) => {
  //           if (response.data.result === 0) {
  //             toastr.success('删除成功！');
  //             $scope.goview('user');
  //           } else if (response.data.result === 1) {
  //             toastr.error(response.data.msg);
  //           }
  //         }).catch((error) => {
  //           $log.error('XHR Failed for getContributors.\n' + angular.toJson(error.data, true));
  //           toastr.error('网络异常，请重试');
  //         });
  //
  //       }
  //     }, function () {
  //       $log.info('modal-component dismissed at: ' + new Date());
  //     });
  //   };
  //
  // }

}

